package gdou.gym.sys.log;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

@Slf4j
@Component
@Aspect
public class LogConfig {
    @Pointcut("@annotation(gdou.gym.sys.log.Log)")
    public void myLogPointcut() {
    }


    @Before("myLogPointcut()")
    public void doBefore(JoinPoint joinPoint) {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            log.error("获取请求url失败");
        } else {
            HttpServletRequest request = attributes.getRequest();
            log.info("请求Url : {}", request.getRequestURL().toString());
        }
        Parameter[] parameters = method.getParameters();
        log.info("方法名是: {}.{}", method.getDeclaringClass().getName(), method.getName());
        log.info("方法参数类型: {}", Arrays.toString(parameters));
        log.info("参数值是: {}", Arrays.toString(joinPoint.getArgs()));
    }


}
